package com.tz.yiqingapp.controller.doctor;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tz.yiqingapp.bean.Doctor;
import com.tz.yiqingapp.bean.Doctor;
import com.tz.yiqingapp.config.AccessLimit;
import com.tz.yiqingapp.mapper.DoctorMapper;
import com.tz.yiqingapp.mapper.DoctorMapper;
import com.tz.yiqingapp.utils.MyPasswordEncoder;
import com.tz.yiqingapp.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.print.Doc;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/doctor")
public class DoctorLogin {

    @Autowired
    DoctorMapper doctorMapper;

    @AccessLimit(times = 5)
    @PostMapping("/login")
    @ResponseBody
    public HashMap<String,Object> login (@RequestBody Doctor doctor, HttpServletRequest request) {
        QueryWrapper<Doctor> doctorQueryWrapper = new QueryWrapper<>();
        doctorQueryWrapper.lambda().eq(Doctor::getName,doctor.getName());
        List<Doctor> doctorList = doctorMapper.selectList(doctorQueryWrapper);
        if(doctorList.size()>0){
            MyPasswordEncoder passwordEncoder = new MyPasswordEncoder();
            if(doctor.getName().equals(doctorList.get(0).getName())
                    && passwordEncoder.matches(doctor.getPassword(),doctorList.get(0).getPassword()) ){
                request.getSession().setAttribute("loginrole","doctor");
                request.getSession().setAttribute("loginid",doctorList.get(0).getId());
                System.out.println("sessionid :" + request.getSession().getId());
                return ResponseUtils.success("login success:" ); // 2021nngdf
            }else{
                return ResponseUtils.fail("username,password error");

            }
        }
       return ResponseUtils.fail("login fail,user not found ");
    }
    @AccessLimit(times = 5)
    @PostMapping("/regist")
    @ResponseBody
    public HashMap<String,Object> regist (@RequestBody Doctor doctor, HttpServletRequest request) {
        QueryWrapper<Doctor> doctorQueryWrapper = new QueryWrapper<>();
        doctorQueryWrapper.lambda().eq(Doctor::getName,doctor.getName());
        List<Doctor> doctorList = doctorMapper.selectList(doctorQueryWrapper);
        if(doctorList.size()>0){
            return ResponseUtils.fail("user exited");
        }
        doctor.setPassword(MyPasswordEncoder.doencode(doctor.getPassword()));
        doctorMapper.insert(doctor);
        return ResponseUtils.success("regist success");
    }

    @AccessLimit(times = 5)
    @PostMapping("/logout")
    public HashMap<String,Object> logout (@RequestBody Doctor doctor, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("doctor")) {
            request.getSession().setAttribute("loginrole",null);
            return ResponseUtils.fail("login out success ");
        }
        return ResponseUtils.notlogin("login fail ");
    }
    @AccessLimit(times = 5)
    @PostMapping("/changepass")
    public HashMap<String,Object> changepass (@RequestBody Doctor doctor, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("doctor")){

            long uid = (long) request.getSession().getAttribute("loginid");
            doctor.setId(uid);
            doctorMapper.updateById(doctor);
        }
        return ResponseUtils.notlogin("login fail");
    }

    @AccessLimit(times = 5)
    @PostMapping("/getUserBaseInfo")
    public HashMap<String,Object> getUserBaseInfo (@RequestBody Doctor doctor, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("doctor")){
            return  ResponseUtils.success(doctorMapper.selectById((long)request.getSession().getAttribute("loginid")));
        }
        return ResponseUtils.notlogin("login fail");
    }
}
